/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gamaze;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author pborrego
 */
public class CellRenderer extends JPanel {
    public CellRenderer(int perferredCell, int perferredWall) {
        super();
//        this.setPreferredSize(new Dimension(perferredCell, perferredCell));
//        this.setSize(new Dimension(perferredCell, perferredCell));
//        this.wallSize = perferredWall;
//        this.setLayout(new FlowLayout());

        this.lblType = new JLabel();
        this.lblType.setHorizontalAlignment(JLabel.CENTER);
        this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.red));
    }

    public int getRestrictionCode() {
        return restrictionCode;
    }

    public void setRestrictionCode(int restrictionCode) {
        this.restrictionCode = restrictionCode;

        int size = 1;
        int east = Maze.E != (this.restrictionCode&Maze.E) ? size : 0;
        int south = Maze.S != (this.restrictionCode&Maze.S) ? size : 0;
        int top = CellRenderer.BORDER_TOP == (this.cellType&CellRenderer.BORDER_TOP) ? size : 0;
        int left = CellRenderer.BORDER_LEFT == (this.cellType&CellRenderer.BORDER_LEFT) ? size : 0;
        this.setBorder(BorderFactory.createMatteBorder(top, left, south, east, Color.red));
    }

    public void clearCellType() {
        this.cellType = CellRenderer.EMPTY;
        this.remove(this.lblType);
    }

    public void setCellType(int type) {
        this.cellType |= type;

        if( CellRenderer.START == (this.cellType&CellRenderer.START) ) {
            this.lblType.setText("S");
        }

        if( CellRenderer.END == (this.cellType&CellRenderer.END) ) {
            this.lblType.setText("E");
        }

        if( CellRenderer.CURSOR == (this.cellType&CellRenderer.CURSOR) ) {
            this.lblType.setText("*");
        }

        this.add(this.lblType);
    }

    public int getCellTYpe() {
        return this.cellType;
    }


    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        super.paintComponent(g);

        g2.setPaint(Color.black);

        if(CellRenderer.PATH == (this.cellType&CellRenderer.PATH)) {
            this.setBackground(Color.blue);
        }
    }

//    private int wallSize = 0;
    private int restrictionCode = 15;
    private int cellType = CellRenderer.EMPTY;
    public final static int EMPTY = 0;
    public final static int START = 1;
    public final static int END = 2;
    public final static int PATH = 4;
    public final static int CURSOR = 8;
    public final static int BORDER_TOP = 16;
    public final static int BORDER_LEFT = 32;
    private JLabel lblType;

}
